#include "ParticleObserver.h"

#include "ParticleContext.h"
#include "DescriptorParticle.h"

#include "Persecutor.h"

using namespace ParticleEngine; 

const DescriptorParticle ParticleObserver::Descriptor
(
	"Observer", 
	[](ParticleContext& NewContext) { return new ParticleObserver(NewContext); }
); 

ParticleObserver::ParticleObserver(ParticleContext& NewContext) :
ParticleBase(NewContext, &ParticleObserver::Descriptor),
Heat(0.5f),
IsFocused(false),
PressureCount(0)
{
	Adhesions = new PropertyAdhesions(0.1f);
	Adhesions->ParticleCast = this;
}

ParticleObserver::~ParticleObserver()
{
	if (IsFocused == true)
		Context.Observer.Unbind();
}

void ParticleObserver::Sole()
{
	if (IsFocused == true)
	{
		if (PressureCount > 0)
		{
			const double AvgPressure = Pressure / (double)(PressureCount);
			Pressure = 0;
			Context.Observer.Audio.AddSignal(AvgPressure);
		}
		else
			Context.Observer.Audio.AddSignal(0);
	}
	PressureCount = 0;

	this->SoleStandard();
	this->Heat.Sole();
}
void ParticleObserver::InteractionIndividual(ParticleBase*const Other, Vec<2, float>& Space, const float& DistanceSquared)
{
	Pressure += DistanceSquared;
	PressureCount += 1;

	this->InteractionKinetics(Other, Space, DistanceSquared);
	Heat.Interaction(Other->HasHeat());
}

ColorRGBA<unsigned char> ParticleObserver::GetColor()
{
	return ColorRGBA<unsigned char> (0, 255, 255, 255);
}

void ParticleObserver::SaveIndividual(std::ofstream& Strm)
{
	Heat.Save(Strm);
}
void ParticleObserver::LoadIndividual(std::ifstream& Strm)
{
	Heat.Load(Strm);
}


